package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.DigitalSliceCaseCollectLikeMapper;
import com.zhijian.medical.entity.po.DigitalSliceCaseCollectLike;
import com.zhijian.medical.entity.vo.request.DigitalSliceCaseCollectLikeSearchVo;
import com.zhijian.medical.entity.vo.response.DigitalSliceCaseCollectLikeVo;
import com.zhijian.medical.enums.DigitalSliceCaseCollectLikeEnum;
import com.zhijian.medical.service.DigitalSliceCaseCollectLikeService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 数字切片-病例珍藏or点赞业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-12-31 18:18:42
 */
@Slf4j
@Service
public class DigitalSliceCaseCollectLikeServiceImpl implements DigitalSliceCaseCollectLikeService {

    @Autowired
    private DigitalSliceCaseCollectLikeMapper mapper;

    @Override
    public DigitalSliceCaseCollectLike selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DigitalSliceCaseCollectLikeVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByTypeAndCaseIdAndUserId(Byte type, Long caseId, Long userId) {
        mapper.deleteByTypeAndCaseIdAndUserId(type, caseId, userId);
    }

    @Override
    public Long insert(DigitalSliceCaseCollectLike digitalSliceCaseCollectLike) {
        if (digitalSliceCaseCollectLike.getType() == DigitalSliceCaseCollectLikeEnum.COLLECT.getCode().byteValue()) {
            //如果是收藏，就查下之前有没有已经收藏了
            Integer count = this.count(DigitalSliceCaseCollectLikeSearchVo.builder()
                    .caseId(digitalSliceCaseCollectLike.getCaseId())
                    .userId(digitalSliceCaseCollectLike.getUserId())
                    .type(DigitalSliceCaseCollectLikeEnum.COLLECT.getCode().byteValue())
                    .build());
            AssertUtil.check(count > 0, "您已收藏此病例，请勿重复收藏");
        } else if (digitalSliceCaseCollectLike.getType() == DigitalSliceCaseCollectLikeEnum.LIKE.getCode().byteValue()) {
            //如果是点赞，就查下之前有没有已经点赞了
            Integer count = this.count(DigitalSliceCaseCollectLikeSearchVo.builder()
                    .caseId(digitalSliceCaseCollectLike.getCaseId())
                    .userId(digitalSliceCaseCollectLike.getUserId())
                    .type(DigitalSliceCaseCollectLikeEnum.LIKE.getCode().byteValue())
                    .build());
            AssertUtil.check(count > 0, "您已点赞此病例，请勿重复点赞");
        }
        Date now = new Date();
        digitalSliceCaseCollectLike.setCreateTime(now);
        digitalSliceCaseCollectLike.setUpdateTime(now);
        return mapper.insert(digitalSliceCaseCollectLike) > 0 ? digitalSliceCaseCollectLike.getId() : 0L;
    }

    @Override
    public boolean update(DigitalSliceCaseCollectLike digitalSliceCaseCollectLike) {
        Date now = new Date();
        digitalSliceCaseCollectLike.setUpdateTime(now);
        return mapper.updateByPrimaryKey(digitalSliceCaseCollectLike) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(DigitalSliceCaseCollectLike record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(DigitalSliceCaseCollectLikeSearchVo digitalSliceCaseCollectLikeSearchVo) {
        return mapper.count(digitalSliceCaseCollectLikeSearchVo);
    }

    @Override
    public List<DigitalSliceCaseCollectLikeVo> selectList(DigitalSliceCaseCollectLikeSearchVo digitalSliceCaseCollectLikeSearchVo) {
        return mapper.selectList(digitalSliceCaseCollectLikeSearchVo);
    }

    @Override
    public Page<DigitalSliceCaseCollectLikeVo> selectPage(DigitalSliceCaseCollectLikeSearchVo digitalSliceCaseCollectLikeSearchVo) {
        //先直接查总数
        int count = count(digitalSliceCaseCollectLikeSearchVo);
        //分页
        Page<DigitalSliceCaseCollectLikeVo> page = new Page<>(true, count, digitalSliceCaseCollectLikeSearchVo.getPageNum(), digitalSliceCaseCollectLikeSearchVo.getPageSize());
        digitalSliceCaseCollectLikeSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DigitalSliceCaseCollectLikeVo> responseVos = this.selectList(digitalSliceCaseCollectLikeSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<DigitalSliceCaseCollectLike> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}